package com.polukeuro.app.client.manager;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.LoadHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.polukeuro.app.client.events.CustomErrorEvent;
import com.polukeuro.app.client.events.CustomErrorHandler;
import com.polukeuro.app.client.events.GlobalErrorEvent;
import com.polukeuro.app.client.events.InitHandler;
import com.polukeuro.app.client.events.InitStartEvent;
import com.polukeuro.app.client.events.InitStopEvent;

public final class WidgetManager {
	
	private static WidgetManager WM = null;
	
	private WidgetManagerHandler wmh = new WidgetManagerHandler();
	
	private WidgetManager() {
		//singleton
		EventManager.getHandler().addHandler(InitStartEvent.getType(), wmh);
		EventManager.getHandler().addHandler(InitStopEvent.getType(), wmh);
		EventManager.getHandler().addHandler(CustomErrorEvent.getType(), wmh);
		EventManager.getHandler().addHandler(GlobalErrorEvent.getType(), wmh);
	}
	
	public static WidgetManager getInstance() {
		if(WM == null) {
			WM = new WidgetManager();
		}
		return WM;
	}
	
	public void subscribe(Widget widget) {
		if(widget instanceof LoadHandler ) {
			widget.addStyleName("loader");
			widget.fireEvent(new InitStartEvent(widget));
		}
	}
	

	public void showLoader(Widget widget) {
		widget.addStyleName("loading");
	}

	public void hideLoader(Widget widget) {
		widget.removeStyleName("loading");
	}
	
	public void showError(Widget widget) {
		widget.addStyleName("error");
	}
	
	public void showPopinError(String message) {
		RootPanel.get("errorContainer").removeStyleName("invisible");
		//FIXME External widget and uibinded
		final VerticalPanel panel = new VerticalPanel();
		RootPanel.get("globalError").add(panel);
		
	    // Create a DialogBox with a button to close it
		final DialogBox dialogbox = new DialogBox(false, false);
	    dialogbox.setStyleName("errorPopin");
	    dialogbox.setPopupPosition(100, 50);
	    VerticalPanel contents = new VerticalPanel();
	    dialogbox.setText("DialogBox");
	    HTML messageHTML = new HTML(message);
	    Button button = new Button("Close");
	    button.addStyleName("errorPopinButton");
	    button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				panel.clear();
				RootPanel.get("errorContainer").addStyleName("invisible");
			}
		});
	    contents.add(messageHTML);
	    contents.add(button);
	    dialogbox.setWidget(contents);
	    panel.add(dialogbox);
	}

	
	private class WidgetManagerHandler implements InitHandler, CustomErrorHandler{

		public void startInitialisation(InitStartEvent event) {
			WidgetManager.this.showLoader(event.getWidget());
			
		}

		public void stopInitialisation(InitStopEvent event) {
			WidgetManager.this.hideLoader(event.getWidget());
		}

		public void onError(CustomErrorEvent event) {
			WidgetManager.this.showError(event.getWidget());
		}

		public void onGlobalError(GlobalErrorEvent event) {
			WidgetManager.this.showPopinError(event.getMessage());
		}

	}

}
